<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
	
    case 'add' :
        addTopicGroup();
        break;
      
    case 'modify' :
        modifyTopicGroup();
        break;
        
    case 'delete' :
        deleteTopicGroup();
        break;
    
    case 'deleteImage' :
        deleteImage();
        break;
    
	   
    default :
        // if action is not defined or unknown
        // move to main TopicGroup page
        header('Location: index.php');
}


/*
    Add a TopicGroup
*/
function addTopicGroup()
{
	$dv_id   	= (int)$_POST['cboDept'];
	$id   		= (int)$_GET['id'];
    $mann      = $_POST['txtMann'];
	$tennn 	= $_POST['txtTennn'];

	$sql2   = "INSERT INTO nhomnganh(mann, tennn, dv_id)
	          VALUES ('$mann', '$tennn', $dv_id)";

	$result2 = dbQuery($sql2);

	header("Location: index.php");	     
}
/*
    Modify a TopicGroup
*/
function modifyTopicGroup()
{
	$id   		= (int)$_GET['id'];	
	$dv_id   	= (int)$_POST['cboDept'];
    $tennn    		= $_POST['txtTennn'];

	$sql2   = "UPDATE nhomnganh
               SET tennn = '$tennn', dv_id = $dv_id
			   WHERE id = $id" ;	
	dbQuery($sql2);
	//echo $sql2; exit();

	header('Location: index.php');			  
}
/*
    Remove a TopicGroup
*/
function deleteTopicGroup()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}	
		
	$sql1 = "UPDATE nhomnganh SET hieu_luc = 0 WHERE id = $id"; 
	//$sql = "delete from nhomnganh where mann = '$mann'";

	dbQuery($sql1);
		
    header('Location: index.php'); 
}?>